package org.wedding.center.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.StrUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.transaction.annotation.Transactional;
import org.wedding.center.entity.NoticeInfo;
import org.wedding.center.service.NoticeInfoService;
import org.wedding.dto.PageModel;
import org.wedding.center.dto.query.ApprovalQuery;
import org.wedding.center.entity.ApprovalInfo;
import org.wedding.center.mapper.ApprovalInfoMapper;
import org.wedding.center.service.ApprovalInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.wedding.enums.system.ResultCodeEnum;
import org.wedding.utils.CommonResult;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import org.springframework.stereotype.Service;
import cn.hutool.core.util.ObjectUtil;
import org.wedding.utils.PageUtil;

import javax.annotation.Resource;
import java.util.List;

/**
 * 审批申请表(ApprovalInfo)表服务实现类
 *
 * @author songkaiyue
 * @since 2025-05-14 16:25:48
 */
@Service
public class ApprovalInfoServiceImpl extends ServiceImpl<ApprovalInfoMapper, ApprovalInfo> implements ApprovalInfoService {
    @Resource
    private NoticeInfoService noticeInfoService;
    @Value("${manageUrl}")
    private String manageUrl;

    /**
     * 分页查询所有数据
     *
     * @return
     */
    @Override
    public CommonResult<PageModel> findAll(ApprovalQuery query) {
        try {

            //0:普通用户  1: 商家  2:管理员 3超级管理员
            String role = StpUtil.getSession().get("role").toString();
            String userId = "";
            if ("1".equals(role)) {
                userId=StpUtil.getLoginIdAsString();
                query.setApprover(userId);
            }
            List<ApprovalInfo> list = baseMapper.selectAll(query);
            List<ApprovalInfo> setList = query.setList(PageUtil.getListPage(query.getCurrent(), query.getPageSize(), list), list.size());
            query.setList(setList, list.size());
            return CommonResult.success(query);
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed(ResultCodeEnum.API_ERROR);
        }

    }

    /**
     * 根据id查看详情
     *
     * @param id
     * @return
     */
    @Override
    public CommonResult<ApprovalInfo> view(String id) {
        try {
            ApprovalInfo approvalInfo = baseMapper.findById(id);
            return CommonResult.success(approvalInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed(ResultCodeEnum.API_ERROR);
        }
    }

    /**
     * 新增数据
     *
     * @param approvalInfo
     * @return
     */
    @Override
    public CommonResult add(ApprovalInfo approvalInfo) {
        try {
            approvalInfo.setId(IdUtil.simpleUUID());
            approvalInfo.setCreateTime(DateUtil.now());
            approvalInfo.setStatus("0");
            approvalInfo.setDelFlag("0");
            String userId = StpUtil.getSession().get("userId").toString();
            String userName = StpUtil.getSession().get("userName").toString();
            approvalInfo.setCreator(userId);
            approvalInfo.setCreatorName(userName);
            baseMapper.insert(approvalInfo);
            return CommonResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed(ResultCodeEnum.API_ERROR);
        }
    }

    /**
     * 修改数据
     *
     * @param approvalInfo
     * @return
     */
    @Override
    @Transactional
    public CommonResult updateEntity(ApprovalInfo approvalInfo) {
        try {
            if (StrUtil.isEmpty(approvalInfo.getId())) {
                return CommonResult.failed(ResultCodeEnum.PARAM_ERROR);
            }
            if (StrUtil.isNotEmpty(approvalInfo.getStatus())) {
                //审批申请
                String userId = StpUtil.getSession().get("userId").toString();
                String userName = StpUtil.getSession().get("userName").toString();
                approvalInfo.setApprover(userId);
                approvalInfo.setApproverName(userName);
                if ("1".equals(approvalInfo.getStatus())) {
                    //审批通过-通知
                    if ("0".equals(approvalInfo.getBusinessType())) {
                        //商家入驻审批通过-发送站内信
                        noticeInfoService.add(NoticeInfo.builder()
                                .userId(approvalInfo.getCreator())
                                .userName(approvalInfo.getCreatorName())
                                .notice("您的商家入驻申请已通过,请前往商家管理端进行查看\n" + "管理端地址: " + manageUrl + "\n用户名: " + approvalInfo.getCreatorName() + "\n密码: 123456")
                                .type("3")
                                .quality("high")
                                .creator(StpUtil.getLoginIdAsString())
                                .build());
                    }
                    if ("1".equals(approvalInfo.getBusinessType())) {
                        //提现申请审批
                        noticeInfoService.add(NoticeInfo.builder()
                                .userId(approvalInfo.getCreator())
                                .userName(approvalInfo.getCreatorName())
                                .notice("您的提现申请已通过,请查看您的个人账户是否到账！")
                                .type("3")
                                .quality("high")
                                .creator(StpUtil.getLoginIdAsString())
                                .build());
                    }
                } else if ("2".equals(approvalInfo.getStatus())) {
                    //审批不通过-通知
                    if ("0".equals(approvalInfo.getBusinessType())) {
                        //商家入驻审批不通过-发送站内信
                        noticeInfoService.add(NoticeInfo.builder()
                                .userId(approvalInfo.getCreator())
                                .userName(approvalInfo.getCreatorName())
                                .notice("您的商家入驻申请未通过,原因为【" + approvalInfo.getRemark() + "】请修改您的商户信息再重新提交申请！")
                                .type("3")
                                .quality("high")
                                .creator(StpUtil.getLoginIdAsString())
                                .build());
                    }
                    if ("1".equals(approvalInfo.getBusinessType())) {
                        //提现申请审批
                        noticeInfoService.add(NoticeInfo.builder()
                                .userId(approvalInfo.getCreator())
                                .userName(approvalInfo.getCreatorName())
                                .notice("您的提现申请未通过,原因为【" + approvalInfo.getRemark() + "】！")
                                .type("3")
                                .quality("high")
                                .creator(StpUtil.getLoginIdAsString())
                                .build());
                    }
                }
            }
            approvalInfo.setUpdateTime(DateUtil.now());
            baseMapper.updateById(approvalInfo);
            return CommonResult.success(approvalInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed(ResultCodeEnum.API_ERROR);
        }
    }

    /**
     * 删除数据
     *
     * @param ids
     * @return
     */
    @Override
    public CommonResult delete(List<String> ids) {
        try {
            if (ObjectUtil.isEmpty(ids)) return CommonResult.failed(ResultCodeEnum.PARAM_ERROR);
            for (String id : ids) {
                baseMapper.upDeleteById(id);
            }
            return CommonResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed(ResultCodeEnum.API_ERROR);
        }
    }

    @Override
    public ApprovalInfo findBybusinessIDAndType(String id, String type) {
        return baseMapper.findBybusinessIDAndType(id, type);
    }

}
